package com.wteam.controller.common;

import com.wteam.framework.common.cache.limit.annotation.LimitPoint;
import com.wteam.framework.common.enums.ResultCode;
import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.exception.ServiceException;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.member.entity.entity.SdMember;
import com.wteam.framework.modules.member.service.MemberService;
import com.wteam.framework.modules.member.service.SdMemberService;
import com.wteam.framework.modules.sms.SmsUtil;
import com.wteam.framework.modules.verification.entity.enums.VerificationEnums;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @author doncic
 * @date 2023/7/17 13:43
 */
@Tag(name = "用户相关接口")
@Slf4j
@RestController
@RequestMapping("/api/member")
public class MemberController {

    @Autowired
    private SdMemberService sdMemberService;
    @Autowired
    private SmsUtil smsUtil;
    @Autowired
    private MemberService memberService;


    /**
     * 用户详细信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "用户信息查询")
    @GetMapping("/{id}")
    public ResultMessage getMemberDetails(@PathVariable("id") Long id) {
        return sdMemberService.getMemberDetails(id);
    }

    /**
     * 允许用户编辑和管理个人资料，包括头像、个人简介等信息。
     * @return
     */

    @Operation(summary = "修改用户资料")
    @PutMapping("/updateMember/{id}")
    public ResultMessage updateMember(@PathVariable("id") Long id, @RequestBody SdMember sdMember) {
        sdMember.setId(id);
        System.out.println(sdMember);
        return sdMemberService.updateById(sdMember)
                ? ResultUtil.success()
                : ResultUtil.error(ResultCode.UPDATE_MEMBER_ERROR);
    }

    /**
     * 发送短信验证码接口
     * @param uuid
     * @param mobile
     * @param verificationEnums
     * @return
     */

//     限制同一个ip短信发送次数的限制注释
    @LimitPoint(name = "sms_send", key = "sms")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, schema = @Schema(type = "string"), name = "mobile", description = "手机号"),
            @Parameter(in = ParameterIn.HEADER, schema = @Schema(type = "string"), name = "uuid", description = "uuid"),
    })
    @GetMapping("/{verificationEnums}/{mobile}")
    @Operation(summary = "发送短信验证码,一分钟同一个ip请求1次")
    public ResultMessage getSmsCode(
            @RequestHeader String uuid,
            @PathVariable String mobile,
            @PathVariable VerificationEnums verificationEnums) {

        smsUtil.sendSmsCode(mobile, verificationEnums, uuid);
        return ResultUtil.success(ResultCode.VERIFICATION_SEND_SUCCESS);
    }

    /**
     * 短信登录接口
     * @param mobile
     * @param code
     * @param uuid
     * @return
     */



    @Operation(summary = "短信登录接口")
    @Parameters({
            @Parameter(name = "mobile", description = "手机号", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "code", description = "验证码", required = true, in = ParameterIn.QUERY)
    })
    @PostMapping("/smsLogin")
    public ResultMessage<Object> smsLogin(@NotNull(message = "手机号为空") @RequestParam String mobile,
                                          @NotNull(message = "验证码为空") @RequestParam String code,
                                          @RequestHeader String uuid) {
        if (smsUtil.verifyCode(mobile, VerificationEnums.LOGIN, uuid, code)) {
            return ResultUtil.data(sdMemberService.mobilePhoneLogin(mobile));
        } else {
            throw new ServiceException(ResultCode.VERIFICATION_SMS_CHECKED_ERROR);
        }
    }

    /**
     * 超级管理员屏蔽用户接口
     * @param userId
     * @param mobile
     * @return
     */

    @Operation(summary = "屏蔽用户接口")
    @Parameters({
            @Parameter(name = "mobile", description = "手机号", required = true, in = ParameterIn.QUERY)
    })
    @PostMapping("/block")
    public ResponseEntity<String> blockUser(@RequestHeader Long userId,
                                            @RequestParam String mobile) {
        // 调用UserService的方法来屏蔽用户
        sdMemberService.blockUser(userId,mobile);
        return ResponseEntity.ok("用户已被屏蔽");
    }

    /**
     * 超级管理员页面排序接口
     * @param userId
     * @param order
     * @param mobile
     * @return
     */

    @Operation(summary = "页面排序接口")
    @Parameters({
            @Parameter(name = "mobile", description = "手机号", required = true, in = ParameterIn.QUERY)
    })
    @PostMapping("/order")
    public ResponseEntity<String> modelOrder(@RequestHeader Long userId,
                                             @RequestParam Long order,
                                             @RequestParam String mobile) {
        // 调用UserService的方法来屏蔽用户
        sdMemberService.modelOrder(userId,mobile,order);
        return ResponseEntity.ok("排序输入成功");
    }







}
